<?php

use console\db\Migration;
use yii\db\Query;

/**
 * 商城订单退款交易信息转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m200210_091353_u_filter extends Migration {

    /**
     * @var string 临时增加内存
     */
    public $memorySize = '512M';

    /**
     * 自定义查询
     * @inheritdoc
     */
    public function safeUp() {
        $query = (new Query())->from('{{%company}}');
        $this->handleProgress($query, [$this, 'evenDule']);
    }

    /**
     * 处理结果
     * @param Migration $migration
     * @param array $rows
     */
    public static function evenDule($migration, $rows) {
        if (empty($rows)) {
            return;
        }
        $migration->compact = true;
        foreach ($rows as $row) {
            $host = [
                '供电公司',
                '国网',
                '公司',
            ];
            $name = $row['name'] ? str_replace($host, '', $row['name']) : $row['name'];
            $columns['name'] = $name;
            $migration->update('{{%company}}', $columns, [
                'id' => $row['id'],
            ]);
        }
    }

}
